package com.lanou;

import jdk.internal.dynalink.linker.LinkerServices;

import java.util.*;

/**
 * @author zyj
 * @date 2022年05月24日 16:53
 */
public class 斗地主 {
    public static void main(String[] args) {
        String[] cardStyle = {"♦", "♣", "♠", "♥"};
        String[] playBoys = {"小花", "小边", "小乔"};
        List<String> cards = new ArrayList<>();
        cards.add("大王");
        cards.add("小王");
        Random random = new Random();
        for (String s : cardStyle) {
            Set<String> cardSet = new HashSet<>();
            while (cardSet.size() != 13) {
                int num = random.nextInt(13) + 1;
                cardSet.add(s + num);
            }
            cards.addAll(cardSet);
        }
        System.out.println(cards.size());

        //洗牌
        Collections.shuffle(cards);

        //获取三张地主牌
        Set<String> diZhu = new HashSet<>();

        while (diZhu.size() < 3) {
            int index = random.nextInt(cards.size());
            diZhu.add(cards.remove(index));
        }

        Map<String, List<String>> plays = new HashMap<>();

        for (String name : playBoys) {
            List<String> play = new ArrayList<>();
            while (play.size() < 17) {
                int index = random.nextInt(cards.size());
                play.add(cards.remove(index));
            }

            plays.put(name, play);

        }
        cards.forEach(v -> {
            System.out.print(v + " ");
        });
        System.out.println();
        System.out.print("地主牌：");
        diZhu.forEach(v -> {
            System.out.print(v + " ");
        });


        System.out.println("获取地主");
        String name = playBoys[random.nextInt(playBoys.length)];
        System.out.println("地主：" + name);
        plays.get(name).addAll(diZhu);
        //给牌排序
        plays.forEach((k, v) -> {
            v.sort((s1,s2) ->{
                int num = getNum(s1);
                int num1 = getNum(s2);

                if (num - num1 > 0){
                    return 1;
                }else if(num - num1 < 0){
                    return -1;
                }else {
                    char style1 = s1.charAt(0);
                    char style2 = s2.charAt(0);
                    if (style1 - style2 > 0){
                        return 1;
                    }else if(style1 - style2 < 0){
                        return -1;
                    }else {
                        return 0;

                    }
                }
            });
        });
        System.out.println();
        plays.forEach((k, v) -> {
            System.out.print(k + ":");
            v.forEach(card -> {
                System.out.print(card + " ");
            });
            System.out.println();
        });

    }

    private static int getNum(String s1) {
        int num = 0;
        try {
            num = Integer.parseInt(s1.substring(1));

        }catch (NumberFormatException e){
            if ("大王".equals(s1)){
                num = 15;
            }else {
                num = 14;
            }
        }

        return num;
    }
}
